package com.huopaolan.yohuoshop.Mvp.Presenter;

import android.util.Log;

import com.huopaolan.lib_core.Mvp.Presenter.BasePresenter;
import com.huopaolan.yohuoshop.Bean.BaseEntity;
import com.huopaolan.yohuoshop.Bean.CategoryAllGoodsEntity;
import com.huopaolan.yohuoshop.Bean.CategoryGoodsEntity;
import com.huopaolan.yohuoshop.Mvp.Contract.CategoryFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class CategoryFragmentPresenter extends BasePresenter<CategoryFragmentContract.ICategoryView, CategoryFragmentContract.ICategoryModel> {
    protected static int page = 1;

    @Inject
    public CategoryFragmentPresenter(CategoryFragmentContract.ICategoryView iCategoryView, CategoryFragmentContract.ICategoryModel iCategoryModel) {
        super(iCategoryView, iCategoryModel);
    }

    //发起请求
    public void requestClassify() {
        page = 1;
        Log.d("requestClassify", "请求成功");
//        Map<String, String> map = new HashMap<>();
        JSONObject goods = new JSONObject();
        try {
            goods.put("categoryid", page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = goods.toString();
        m.requestGoods(request).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseEntity baseEntity) {
                        if (baseEntity instanceof CategoryGoodsEntity) {
                            v.goodsSuccess((CategoryGoodsEntity) baseEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("onError", "请求失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //发起全部请求
    public void requestAll() {
        Log.d("requestAll", "请求成功");
//        Map<String, String> map = new HashMap<>();
        JSONObject all = new JSONObject();
        try {
            all.put("menuid", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = all.toString();
        m.requestGoodsAll(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseEntity baseEntity) {
                        Log.d("onNext", "请求成功");
                        if (baseEntity instanceof CategoryAllGoodsEntity) {
                            v.goodsAllSuccess((CategoryAllGoodsEntity) baseEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
